{
    "cells": [
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## **Applio**\n",
                "A simple, high-quality voice conversion tool focused on ease of use and performance.\n",
                "\n",
                "[Support](https://discord.gg/urxFjYmYYh) — [Discord Bot](https://discord.com/oauth2/authorize?client_id=1144714449563955302&permissions=1376674695271&scope=bot%20applications.commands) — [Find Voices](https://applio.org/models) — [GitHub](https://github.com/IAHispano/Applio)\n",
                "\n",
                "<br>\n",
                "\n",
                "### **Credits**\n",
                "- Encryption method: [Hina](https://github.com/hinabl)\n",
                "- Main development: [Applio Team](https://github.com/IAHispano)"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## Install"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {
                "trusted": true
            },
            "outputs": [],
            "source": [
                "import codecs\n",
                "import os\n",
                "import tarfile\n",
                "import subprocess\n",
                "from pathlib import Path\n",
                "from IPython.display import clear_output\n",
                "rot_47 = lambda encoded_text: \"\".join(\n",
                "    [\n",
                "        (\n",
                "            chr(\n",
                "                (ord(c) - (ord(\"a\") if c.islower() else ord(\"A\")) - 47) % 26\n",
                "                + (ord(\"a\") if c.islower() else ord(\"A\"))\n",
                "            )\n",
                "            if c.isalpha()\n",
                "            else c\n",
                "        )\n",
                "        for c in encoded_text\n",
                "    ]\n",
                ")\n",
                "\n",
                "new_name = rot_47(\"kmjbmvh_hg\")\n",
                "findme = rot_47(codecs.decode(\"pbbxa://oqbpcj.kwu/Dqlitvb/qurwg-mtnqvlmz.oqb\", \"rot_13\"))\n",
                "uioawhd = rot_47(codecs.decode(\"pbbxa://oqbpcj.kwu/QIPqaxivw/Ixxtqw.oqb\", \"rot_13\"))\n",
                "!pip install uv\n",
                "!git clone --depth 1 $uioawhd $new_name --branch 3.2.7\n",
                "clear_output()\n",
                "!mkdir -p /kaggle/tmp\n",
                "%cd /kaggle/tmp\n",
                "!uv venv .venv > /dev/null 2>&1\n",
                "def vidal_setup(ForceIn):\n",
                "    def F():\n",
                "        print(\"Installing pip packages...\")\n",
                "        subprocess.check_call([\"uv\", \"pip\", \"install\", \"-r\", \"requirements.txt\", \"--quiet\"])\n",
                "\n",
                "    A = \"/kaggle/working\" + rot_47(\"Kikpm.ovm.bu\")\n",
                "    D = \"/kaggle/tmp\"\n",
                "    if not os.path.exists(A):\n",
                "        M = os.path.dirname(A)\n",
                "        os.makedirs(M, exist_ok=True)\n",
                "        print(\"No cached install found..\")\n",
                "        try:\n",
                "            N = rot_47(codecs.decode(\"pbbxa://pcooqvonikm.kw/QIPqaxivw/Ixxtqw/zmawtdm/uiqv/Mvdqzwumvb/Siootm/SiootmD2.biz.oh?lwevtwil=bzcm\", \"rot_13\"))\n",
                "            subprocess.run([\"wget\",\"-q\" ,\"-O\", A, N])\n",
                "            print(\"Download completed successfully!\")\n",
                "        except Exception as H:\n",
                "            print(str(H))\n",
                "            if os.path.exists(A):\n",
                "                os.remove(A)\n",
                "    if Path(A).exists():\n",
                "        with tarfile.open(A, \"r:gz\") as I:\n",
                "            I.extractall(D)\n",
                "            print(f\"Extraction of {A} to {D} completed.\")\n",
                "        if os.path.exists(A):\n",
                "            os.remove(A)\n",
                "    else:\n",
                "        F()\n",
                "\n",
                "vidal_setup(False)\n",
                "%cd /kaggle/working/program_ml\n",
                "!source /kaggle/tmp/.venv/bin/activate; python core.py \"prerequisites\" --models \"True\" --exe \"True\" --pretraineds_v1_f0 \"False\" --pretraineds_v2_f0 \"True\" --pretraineds_v1_nof0 \"False\" --pretraineds_v2_nof0 \"False\" > /dev/null 2>&1\n",
                "!sudo curl -fsSL https://raw.githubusercontent.com/filebrowser/get/master/get.sh | sudo bash\n",
                "!filebrowser config init\n",
                "!filebrowser config set --auth.method=noauth\n",
                "!filebrowser users add  \"applio\" \"\" --perm.admin\n",
                "clear_output()\n",
                "print(\"Finished\")"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## Setup Ngrok"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {
                "trusted": true
            },
            "outputs": [],
            "source": [
                "#https://dashboard.ngrok.com/get-started/your-authtoken (Token Ngrok)\n",
                "!pip install pyngrok\n",
                "!ngrok config add-authtoken token"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## Start"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {
                "trusted": true
            },
            "outputs": [],
            "source": [
                "import os\n",
                "from pyngrok import ngrok\n",
                "from IPython.display import clear_output\n",
                "ngrok.kill()\n",
                "%cd /kaggle/working/program_ml\n",
                "os.system(f\"filebrowser -r /kaggle -p 9876 > /dev/null 2>&1 &\")\n",
                "clear_output()\n",
                "%load_ext tensorboard\n",
                "%tensorboard --logdir logs --port 8077\n",
                "p_tunnel = ngrok.connect(6969)\n",
                "t_tunnel = ngrok.connect(8077)\n",
                "f_tunnel = ngrok.connect(9876)\n",
                "clear_output()\n",
                "print(\"Applio Url:\", p_tunnel.public_url)\n",
                "print(\"Tensorboard Url:\", t_tunnel.public_url)\n",
                "print(\"File Url:\", f_tunnel.public_url)\n",
                "print(\"Save the link for later, this will take a while...\")\n",
                "\n",
                "!source /kaggle/tmp/.venv/bin/activate; python app.py"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "_kg_hide-input": false
            },
            "source": [
                "## Extra"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## Setup new logs folder format\n",
                "- Put the exact name you put as your Model Name in Applio."
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "\n",
                "modelname = \"Test\"\n",
                "logs_folder = f\"/kaggle/working/program_ml/logs/\" + modelname\n",
                "\n",
                "import os\n",
                "\n",
                "folder_renames = {\n",
                "    \"0_gt_wavs\": \"sliced_audios\",\n",
                "    \"1_16k_wavs\": \"sliced_audios_16k\",\n",
                "    \"2a_f0\": \"f0\",\n",
                "    \"2b-f0nsf\": \"f0_voiced\",\n",
                "    \"3_feature768\": \"v2_extracted\"\n",
                "}\n",
                "\n",
                "def rename_folders(base_path, rename_dict):\n",
                "    for old_name, new_name in rename_dict.items():\n",
                "        old_path = os.path.join(base_path, old_name)\n",
                "        new_path = os.path.join(base_path, new_name)\n",
                "        if os.path.exists(old_path):\n",
                "            os.rename(old_path, new_path)\n",
                "            print(f\"Renamed {old_path} to {new_path}\")\n",
                "        else:\n",
                "            print(f\"Folder {old_path} does not exist\")\n",
                "\n",
                "rename_folders(logs_folder, folder_renames)\n"
            ]
        }
    ],
    "metadata": {
        "kaggle": {
            "accelerator": "nvidiaTeslaT4",
            "dataSources": [],
            "dockerImageVersionId": 30558,
            "isGpuEnabled": true,
            "isInternetEnabled": true,
            "language": "python",
            "sourceType": "notebook"
        },
        "kernelspec": {
            "display_name": "Python 3",
            "language": "python",
            "name": "python3"
        },
        "language_info": {
            "codemirror_mode": {
                "name": "ipython",
                "version": 3
            },
            "file_extension": ".py",
            "mimetype": "text/x-python",
            "name": "python",
            "nbconvert_exporter": "python",
            "pygments_lexer": "ipython3",
            "version": "3.10.12"
        }
    },
    "nbformat": 4,
    "nbformat_minor": 4
}
